package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.dto.MaxFullNameDto;
import com.dingreading.cloud.admin.entity.Members;
import com.dingreading.cloud.admin.entity.table.MembersTableDef;
import com.dingreading.cloud.admin.mapper.MembersMapper;
import com.dingreading.cloud.admin.service.MembersService;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class MembersServiceImpl extends BaseServiceImpl<MembersMapper, Members, MembersTableDef> implements MembersService {

    @Override
    protected MembersTableDef getTable() {
        return MembersTableDef.members;
    }

    @Resource
    private MembersMapper membersMapper;


    @Override
    public Page<Members> pageList(PageUtil pageUtil, String keyword, String storeUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.fullName.like(keyword)
                    .or(table.memberName.like(keyword))
                    .or(table.nickName.like(keyword))
                    .or(table.mob.like(keyword)));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }


    @Override
    public List<Members> getByFullName(String fullName) {
        QueryWrapper wrapper = QueryWrapper.create()
                .where(table.fullName.like(fullName))
                .orderBy(table.id.desc());
        return getMapper().selectListByQuery(wrapper);
    }


    @Override
    public Members haveCorpUidAndMobAndFullName(String corpUid, String mob, String fullName, String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.corpUid.eq(corpUid))
                .and(table.mob.eq(mob))
                .and(table.fullName.eq(fullName));
        if (StringUtils.isNotBlank(uid))
            condition.and(table.uid.ne(uid));
        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public List<Members> listByUidInOrderByStoreUid(List<String> uid) {
        QueryWrapper wrapper = QueryWrapper.create()
                .where(table.uid.in(uid))
                .orderBy(table.storeUid.asc());
        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public MaxFullNameDto maxFullName(String fullName) {
        return membersMapper.maxFullName(fullName);
    }

    @Override
    public List<Members> listByIdBetween(String idMax, String idMin) {
        QueryCondition condition = table.id.between(idMin, idMax)
                .and(table.followTime.isNotNull())
                .and(table.lastFollowUid.isNull());
        return getMapper().selectListByCondition(condition);
    }

    @Override
    public boolean updateLastFollowUid(String memberUid, String followUid) {
        return UpdateChain.of(Members.class)
                .set(table.lastFollowUid, followUid)
                .where(table.uid.eq(memberUid))
                .update();
    }

}
